<template>
  <el-container class="app-container">
    <el-collapse @change="handleCollapseChange" v-model="collapseActiveName">
      <el-collapse-item :title="collapseTitle" name="1">
        <el-tabs type="border-card">
          <el-form :model="pageInfo" ref="form" label-width="100px" size="medium">
            <el-row>
              <el-col :span="8" :offset="0">
                <el-form-item prop="groupId" label="所属办公空间:">
                  <el-select v-model="form.groupId" placeholder="请选择">
                    <el-option
                      v-for="item in spaceOptions"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value">
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8" :offset="0">
                <el-form-item prop="title" label="租户：">
                  <el-input v-model.trim='pageInfo.title' placeholder="请输入名称搜索"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8" :offset="0">
                <el-form-item prop="groupId" label="招商状态:">
                  <el-select v-model="form.groupId" placeholder="请选择">
                    <el-option
                      v-for="item in zsOptions"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value">
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8" :offset="0">
                <el-form-item prop="groupId" label="装修:">
                  <el-select v-model="form.groupId" placeholder="请选择">
                    <el-option
                      v-for="item in zxOptions"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value">
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>


              <el-col :span="8" :offset="1" >
                <el-button type="primary" icon="el-icon-search" @click='getData'>搜索</el-button>
                <!--                  <el-button @click="resetForm">重置</el-button>-->
              </el-col>
            </el-row>
          </el-form>
        </el-tabs>
      </el-collapse-item>
    </el-collapse>
    <el-header class="m-t-20">
      <el-button type="primary" icon="el-icon-plus" @click="addOne">办公空间</el-button>
    </el-header>

    <!--表格-->
    <table-view
      :columns="tableContent"
      :pageList="columnData"

      :templateDiv="{operation:true}"
    >
      <template slot="operation" slot-scope="scope">
        <el-button size="mini" ype="primary" @click="editBtnClick(scope.row ,'view')">详情</el-button>
        <el-button size="mini" type="primary" @click="editBtnClick(scope.row ,'contractDetail')">合同详情</el-button>
        <el-button size="mini" type="primary" @click="editBtnClick(scope.row ,'valueAdd')">增值收益</el-button>
        <el-button size="mini" type="danger" @click="editBtnClick(scope.row ,'delete')">删除</el-button>
      </template>
    </table-view>

    <!--分页-->
    <pagination-view :pagesize="pageInfo.pageSize"
                     :currentPage="pageInfo.pageNumber"
                     :total="total"
                     @size-change="sizeChange"
                     @current-change="pageChange"
    ></pagination-view>

    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="60%"
               :destroy-on-close="true" :before-close="handleClose">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px" style="width: 90%">
        <el-form-item prop="groupId" label="所属办公空间:">
          <el-select v-model="form.groupId" placeholder="请选择">
            <el-option
              v-for="item in spaceOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="num" label="楼号/楼层/房号:">
          <el-input v-model.trim="form.productName" style="width: 200px"></el-input>幢
          <el-input v-model.trim="form.productName" style="width: 200px"></el-input>楼
          <el-input v-model.trim="form.productName" style="width: 200px"></el-input>室
        </el-form-item>
        <el-form-item prop="area" label="面积（m²）:" placeholder="可租赁面积">
          <el-input v-model.trim="form.productName" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item prop="productName" label="装修:">
          <el-radio-group v-model="form.ddd">
            <el-radio-button label="毛坯"></el-radio-button>
            <el-radio-button label="简装"></el-radio-button>
            <el-radio-button label="精装"></el-radio-button>
            <el-radio-button label="其他"></el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item prop="productName" label="预租单价:">
          <el-input v-model.trim="form.productName" style="width: 200px"></el-input>
          <el-radio-group v-model="form.radio">
            <el-radio :label="3">元/㎡/天</el-radio>
            <el-radio :label="6">元/月</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item prop="productName" label="空置起算日:">
          <el-date-picker
            v-model="form.date"
            type="date"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择日期">
          </el-date-picker>
          <span class="form-item-tip">* 即房源成本开始计算的日期（默认为该房源添加的日期）</span>
        </el-form-item>
        <el-form-item prop="productName" label="招商状态:">
          <el-radio-group v-model="form.radio2">
            <el-radio :label="1">可租</el-radio>
            <el-radio :label="2">已租</el-radio>
            <el-radio :label="3">已预订</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item prop="memo" label="房源备注:">
          <el-input v-model.trim="form.memo" type="textarea"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">保存</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <el-dialog title="收益明细" :visible.sync="dialogAddDetail" width="60%"
               :destroy-on-close="true">
      <!--表格-->
      <table-view
        :columns="tableContentsymx"
        :pageList="columnDatasymx"
        :templateDiv="{operation:true}"
      >
        <template slot="operation" slot-scope="scope">
          <el-button size="mini" ype="primary" @click="editBtnClick(scope.row ,'view')">详情</el-button>
          <el-button size="mini" type="primary" @click="editBtnClick(scope.row ,'contractDetail')">合同详情</el-button>
          <el-button size="mini" type="primary" @click="editBtnClick(scope.row ,'valueAdd')">增值收益</el-button>
          <el-button size="mini" type="danger" @click="editBtnClick(scope.row ,'delete')">删除</el-button>
        </template>
      </table-view>
    </el-dialog>
    <el-footer></el-footer>
  </el-container>
</template>

<script>
  import TableView from '@/components/TableView'
  import ImgUpload from '@/components/ImgUpload'

  import PaginationView from '@/components/PaginationView'
  // import pageApi from '@/api/operation/productSetting'

  export default {
    name: 'list',
    components: {
      TableView,
      PaginationView,
      ImgUpload
    },
    data() {
      return {
        dialogAddDetail:false,
        spaceOptions:[],
        zsOptions:[{label:'全部',value:''},{label:'可招商',value:'1'},{label:'不可招商',value:'0'}],
        zxOptions:[{label:'全部',value:''},{label:'装修',value:'1'},{label:'简装',value:'2'},{label:'毛坯',value:'3'},{label:'其他',value:'4'}],
        dialogTitle: '添加',
        dialogVisible: false,
        dialogType: 'add',
        tableContentsymx:[{
          field: 'productName',
          label: '租户主体'
        }, {
          field: 'productName',
          label: '服务项'
        }, {
          field: 'productName',
          label: '金额（收入/支出）'
        }, {
          field: 'productName',
          label: '来源（操作人）'
        }, {
          field: 'productName',
          label: '时间'
        }, {
          field: 'productName',
          label: '备注'
        }],
        columnDatasymx: [{}, {}],
        tableContent: [{
          field: 'productName',
          label: '所属办公空间'
        }, {
          field: 'intro',
          label: '楼层',
          sortable: true
        }, {
          field: 'intro',
          label: '房号',
          sortable: true
        }, {
          field: 'intro',
          label: '面积(m²)',
          sortable: true
        }, {
          field: 'intro',
          label: '租户',
        }, {
          field: 'intro',
          label: '计租日',
          sortable: true
        }, {
          field: 'intro',
          label: '结束日',
          sortable: true
        },{
          field: 'intro',
          label: '合同单价',
          sortable: true
        }, {
          field: 'memo',
          label: '实时单价',
          sortable: true
        } ,{
          field: 'intro',
          label: '下期租金付款日',
          sortable: true
        }, {
          field: 'intro',
          label: '下期物业费付款日',
          sortable: true
        }, {
          field: 'enable',
          label: '状态',
        }, {
          field: 'intro',
          label: '备注',
        },{
          field: 'operation',
          label: '操作',
          width: 400
        },],
        columnData: [{}, {}],
        customWidth: '',
        pageInfo: {
          pageNum: 1,
          pageSize: 20,
          title: ''
        },
        total: 0,
        form: {
          costAmount: '',
          intro: '',
          memo: '',
          title: '',
          addressDetail: '',
          addressName: '',
          saleAmount: '',
          sort: ''
        },
        rules: {
          productName: [
            {required: true, trigger: 'blur', message: '名称不能为空'},
            {min: 1, max: 14, message: '长度不能超过14个汉字'}
          ],
          intro: [
            {required: true, trigger: 'blur', message: '简介不能为空'},
            {min: 1, max: 20, message: '长度不能超过20个汉字'}
          ],
          durationTime: [{required: true, trigger: 'change', message: '请选择套餐时长'}],
          sort: [{required: true, trigger: 'blur', message: '请填写排序'}],
          enable: [{required: true, trigger: 'change', message: '请选择套餐状态'}],
          tag: [{min: 1, max: 4, trigger: 'blur', message: '长度不能超过4个汉字'}],
        }
      }
    },
    watch: {},
    methods: {
      pars(param, key, encode) {
        if (param == null) return '';
        var arr = [];
        var t = typeof (param);
        if (t == 'string' || t == 'number' || t == 'boolean') {
          arr.push(key + '=' + ((encode == null || encode) ? encodeURIComponent(param) : param));
        } else {
          for (var i in param) {
            var k = key == null ? i : key + (param instanceof Array ? '[' + i + ']' : '.' + i);
            arr.push(pars(param[i], k, encode));
          }
        }
        return arr.join("&");
      },
      getMapPoint(){
        console.log(this.dialogTitle);
        window.location.href = `https://3gimg.qq.com/lightmap/components/locationPicker2/index.html?search=1&type=0&key=7JNBZ-7SS3F-CDYJB-JL5R4-I5GME-I6FL2&referer=myapp&backurl=${encodeURIComponent(location.origin + '/spaceManage/list?show=true&dialogTitle=' + this.dialogTitle)}`
      },
      addOne() {
        this.dialogVisible = true
        this.dialogTitle = '添加'
      },

      changeUserStatus: function (id, enable) {
        pageApi.changeEnable(id, enable).then((res) => {
          this.handleMessage(res)
          if (res.code === '200') this.getData()
        })
      },
      onSubmit() {
        let form = this.form
        let dialogType = this.dialogType
        form.duration = form.durationTime + (form.productTypeId == 1 ? '个月' : '天')
        form.enable = Number(form.enable)
        this.$refs['form'].validate((valid) => {
          if (valid) {
            switch (dialogType) {
              case 'add':
                this.dialogTitle = '添加'
                pageApi.create(form).then(res => {
                  this.handleRes(res)
                })
                break
              case 'update':
                this.dialogTitle = '编辑'
                pageApi.update(form).then(res => {
                  this.handleRes(res)
                })
                break
            }
          }
        })
      },
      handleRes(res) {
        this.handleMessage(res)
        if (res.code === '200') {
          this.dialogVisible = false
          this.getData()
        }
      },
      // pageSize变化函数
      sizeChange(val) {
        this.pageInfo.pageSize = val
        this.getData()
      },
      // page变化函数
      pageChange(val) {
        this.pageInfo.pageNumber = val
        this.getData()
      },
      editBtnClick(val, type) {
        switch (type) {
          case 'view':
            pageApi.getById(val.courseProductId).then(res => {
              this.form = Object.assign({}, this.form, res.body)
              let duration = this.form.duration
              let regExp = this.form.productTypeId == 1 ? /个月/ : /天/
              this.form.durationTime = duration.replace(regExp, '')
            })
            this.dialogType = type
            this.dialogVisible = true
            break
          case 'contractDetail':
           this.$router.push({name:'contractDetail',query:{id:val.id}})
            break
          case 'valueAdd':
            this.dialogAddDetail =true
            break
          case 'delete':
            this.changeUserStatus(val.courseProductId, val.enable ? 0 : 1)
            break
        }
      },
      //拉取列表
      getData() {
        pageApi.getList(this.pageInfo).then(res => {
          this.columnData = res.body.records
          this.total = Number(res.body.total)
          this.pageInfo.pageNumber = Number(res.body.pageNumber) || 1
        })
      },
      handleClose(done) {
        let resetForm = {
          costAmount: '',
          courseProductId: '',
          duration: '',
          durationTime: '',
          enable: true,
          intro: '',
          memo: '',
          productName: '',
          saleAmount: '',
          sort: '',
          tag: ''
        }
        this.form = Object.assign({}, this.form, resetForm)
        done()
      }

    },
    mounted() {
      // addr: "浙江省杭州市萧山区金惠路685号"
      // city: "杭州市"
      // latng: "30.185281,120.26323"
      // module: "locationPicker"
      // name: "杭州市萧山区委杭州市萧山区人民政府信访局"
      // show: "true"
      let {addr, latng, name, show, dialogTitle} = this.$route.query
      this.form.addressDetail = addr
      this.form.addressName = name
      this.dialogTitle = dialogTitle
      if (show) {
        this.dialogVisible = true
      }
      this.getData()
    }
  }
</script>

<style lang="scss" scoped>

</style>
